package cn.tedu.ivos.base.aop;


import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;


@Aspect  //必须添加该注解，表示该类是切面类
@Component
@Slf4j
public class LogAspect {
    /*
      * 表达式
      * execution("连接点(方法)")
      * bean --指定bean对象
      * within -- 指定到类
      * @annotation
     */
    //execution(public JsonResult cn.tedu.ivos.vehicle.controller.VehicleController.selectVehicle(VehicleQuery))
    //可以模糊匹配：*通配，方法中的参数用..
//    @Pointcut("execution(public * cn.tedu.ivos.*.controller.*.*(..))")
    //@Pointcut("bean(userController)")
   @Pointcut("within(cn.tedu.ivos.application.controller.ApplicationController)")
  //  @Pointcut("@annotation(Log)")
    public void anyController(){}

//    @Before("anyController()")
//    public void before(JoinPoint joinPoint){
//        requestUrl();
//        // 请求参数
//        Object[] args = joinPoint.getArgs();
//        Stream<?> stream = ArrayUtils.isEmpty(args) ? Stream.empty() : Arrays.stream(args);
//        List<Object> logArgs = stream
//                .filter(arg -> (!(arg instanceof HttpServletRequest) && !(arg instanceof HttpServletResponse) &&
//                        !(arg instanceof MultipartFile)))
//                .collect(Collectors.toList());
//        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
//        // 类路径URL
//        String methodPackage = methodSignature.getDeclaringTypeName();
//        // 方法名
//        String method = methodSignature.getMethod().getName();
//        String builder = methodPackage +
//                "." +
//                method +
//                "，请求参数：" +
//                JSON.toJSONString(logArgs);
//        log.info(builder);
//    }
//
    @After("anyController()")
    public void after(JoinPoint joinPoint){
        requestUrl();
        log.info("这是后置通知");
    }
//    @AfterThrowing("anyController()")
//    public void afterThrowing(JoinPoint joinPoint){
//        requestUrl();
//        log.info("这是异常通知");
//    }
//    @AfterReturning("anyController()")
//    public void afterReturning(JoinPoint joinPoint){
//        requestUrl();
//        log.info("这是最终通知(返回通知)");
//    }
//    @Around("anyController()")
//    public void around(ProceedingJoinPoint proceed){
//        Object result = null;
//        //获取方法名称
//        String methodName = proceed.getSignature().getName();
//        try {
//            log.info("这个方法的方法名为："+methodName+"开始运行，参数为："+
//                    Arrays.asList(proceed.getArgs()));
//            ArrayList<Object> list = new ArrayList<>();
//            for(int i=0;i<=10;i++){
//                list.add(i);
//            }
//            System.out.println(list);
//            result = proceed.proceed();
//            log.info("这个方法最终执行完毕，结果为：{}",result);
//        }catch (Throwable a){
//            log.info("这个方法出异常了");
//        }finally {
//            log.info("方法名为："+methodName+"运行结束");
//        }
//    }
    //打印请求的路径
    public void requestUrl(){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if(requestAttributes!=null){
            HttpServletRequest request = requestAttributes.getRequest();
            String requestURI = request.getRequestURI();
            log.info(String.format("请求路径uri: %s",requestURI));
        }
    }
}
